import java.util.Arrays;


public class LetterInventory 
{

	public static final int ALPHANUM = 26;
	public static final int toZero = 97;
	int totalCount = 0;
	int[] alphaCounters = new int[ALPHANUM];
	
	public LetterInventory(String data)
	{
		Arrays.fill(alphaCounters, 0);
		data = data.toLowerCase();
		for(int x = 0; x < (data.length()); x++)
		{
			if((data.charAt(x)) >= 'a' && data.charAt(x) < ('a' + ALPHANUM))
			{
				alphaCounters[(data.charAt(x)) - toZero]++;
				totalCount++;
			}
			
		}
	}
	
	public int get(char letter)
	{
		letter = Character.toLowerCase(letter);
		if(letter >= 'a' && letter < ('a' + ALPHANUM))
		{
			return alphaCounters[(letter - toZero)];
		}
		else
		{
			throw new IllegalArgumentException("Boo you whore");
		}
	}
	
	public void set(char letter, int value)
	{
		letter = Character.toLowerCase(letter);
		if(letter >= 'a' && letter < ('a' + ALPHANUM) && value >= 0)
		{
			totalCount -= alphaCounters[(letter - toZero)];
			alphaCounters[(letter - toZero)] = value;
			totalCount += value;
		}
		else if(value < 0)
		{
			throw new IllegalArgumentException("negative number");
		}
		else
		{
			throw new IllegalArgumentException("non alpha char");
		}
	}
	
	public int size()
	{
		return totalCount;
	}
	
	public boolean isEmpty()
	{
		return(totalCount == 0);
	}
	
	public String toString()
	{
		String holder = "[]";
		if(totalCount > 0)
		{
			holder = "[";
			for(int x = 0; x < ALPHANUM; x++)
			{
				if(alphaCounters[x] > 0)
				{
					for(int i = 0; i < alphaCounters[x]; i++)
					{
						holder += "" + (char) (x + 97);
					}
				}
			}
			holder += "]";
		}
		return holder;
	}
	
	public LetterInventory add(LetterInventory other)
	{
		String combined = "";
		int sum = 0;
		LetterInventory inventorySum = new LetterInventory(combined);
		for(int x = 0; x < ALPHANUM; x++)
		{
			sum = alphaCounters[x] + other.alphaCounters[x];
			if(sum > 0)
			{
				inventorySum.set((char) (x + toZero), sum);
			}
		}
		return inventorySum;
	}
	
	public LetterInventory subtract(LetterInventory other)
	{
		String combined = "";
		int sub = 0;
		LetterInventory inventorySub = new LetterInventory(combined);
		for(int x = 0; x < ALPHANUM; x++)
		{
			sub = alphaCounters[x] - other.alphaCounters[x];
			if(sub >= 0)
			{
				inventorySub.set((char) (x + toZero), sub);
			}
			else
			{
				return null;
			}
		}
		return inventorySub;
	}
	
}
